//
//  YYAnimatedImageView.m
//  Client
//
//  Created by xia on 2020/10/29.
//  Copyright © 2020 xia. All rights reserved.
//

#import "YYAnimatedImageView+Layer.h"
#import <objc/message.h>
@implementation YYAnimatedImageView (Layer)

+ (void)load {
    
//    Method displayLayerMethod = class_getInstanceMethod(self, @selector(displayLayer:));
//
//    Method displayLayerNewMethod = class_getInstanceMethod(self, @selector(displayLayerNew:));
//
//    method_exchangeImplementations(displayLayerMethod, displayLayerNewMethod);
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

      Method method1;

      Method method2;

      method1 = class_getInstanceMethod([self class], @selector(lz_displayLayer:));

      method2 = class_getInstanceMethod([self class], @selector(displayLayer:));

      method_exchangeImplementations(method1, method2);
    });
}

//- (void)displayLayerNew:(CALayer *)layer {
//
//    Ivar imgIvar = class_getInstanceVariable([self class], "_curFrame");
//    UIImage *img = object_getIvar(self, imgIvar);
//    if (img) {
//        layer.contents = (__bridge id)img.CGImage;
//    } else {
//        if (@available(iOS 14.0, *)) {
//            [super displayLayer:layer];
//        }
//    }
//}
-(void)lz_displayLayer:(CALayer *)layer {

Ivar ivar = class_getInstanceVariable(self.class, "_curFrame");

UIImage *_curFrame = object_getIvar(self, ivar);

if (_curFrame) {

  layer.contents = (__bridge id)_curFrame.CGImage;
}else{

  if (@available(iOS 14.0, *)) {

      [super displayLayer:layer];

  }
}

}

@end
